﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vngoodlife.Singleton;
using vngoodlife.Util;

namespace vngoodlife.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            //Delete cookie
            if (Request.Cookies[ConstantClass.cooPageNumberOfHomePage] != null)
            {
                Response.Cookies[ConstantClass.cooPageNumberOfHomePage].Expires = DateTime.Now.AddDays(-1);
            }

            Session["hihi"] = "đây la giá trị của session "+DateTime.Now;

            //return View("/Views/FrontEnd/Home/Index.cshtml");

            /////////// get 5 random article////////////
            List<Article> randomArticle = new List<Article> { };
            var rand = new Random();
            int lastID = DataSingleton.Instance.GetLinQContext().Articles.OrderByDescending(u=>u.ArticleID).First().ArticleID;
            while (randomArticle.Count() < 5)
            {
                var x = DataSingleton.Instance.GetLinQContext().Articles
                        .SingleOrDefault(
                                  u => u.ArticleVisible == true
                                  && (u.ArtStatusID == 3 || u.ArtStatusID == 4)
                                  && u.ArticleID == rand.Next(lastID)
                                 );

                if (x != null && !randomArticle.Contains(x))
                {
                    randomArticle.Add(x);
                }
            }
            /////////// END get 5 random article////////////

            List<ArticleView> tempResult = new List<ArticleView> { };
            List<ArticleView> tempCarrier = new List<ArticleView> { };
            foreach (var item in DataSingleton.Instance.GetLinQContext().ArticleViews.Where(u => u.ArticleVisible == true
                                               && (u.ArtStatusID == 3 || u.ArtStatusID == 4) && u.ArticleTypeID!=138)
                                               .OrderByDescending(u=>u.ArticleID))
            {
                if (!tempResult.Select(u => u.ArticleTypeID).Contains(item.ArticleTypeID)
                    && !tempResult.Select(u => u.ArticleID).Contains(item.ArticleID)
                    && !tempCarrier.Select(u => u.ArticleID).Contains(item.ArticleID)
                    && !tempCarrier.Select(u => u.ArticleTypeID).Contains(item.ArticleTypeID)
                    )
                {
                    tempResult.Add(item);
                }
                else
                {
                    tempCarrier.Add(item);
                }
                if (tempResult.Count() == 5)
                {
                    break;
                }
            }

            ViewBag.Tap1 = DataSingleton.Instance.GetLinQContext().Articles
                           .Where(u => tempResult.Select(x => x.ArticleID).Contains(u.ArticleID))
                           .OrderByDescending(u => u.ArticleID);
        
            /////////// END Tap1 ////////////

            //Remember 5 new articleID to not show in article list home page
            DataSingleton._listNewArticleIDsOfEachArticleType = new List<int> { };
            foreach (var item in ViewBag.Tap1 as IQueryable<Article>)
            {
                DataSingleton._listNewArticleIDsOfEachArticleType.Add(item.ArticleID);
            }

            /////////// Tap2 ////////////
            IQueryable<Article> tempTap2 = DataSingleton.Instance.GetLinQContext().Articles
                                                       .Where(
                                                              u => u.ArticleVisible == true
                                                              && (u.ArtStatusID == 3 || u.ArtStatusID == 4)
                                                              && (u.CreateDate > DateTime.Now.AddDays(-7)) 
                                                             )
                                                       .OrderByDescending(u => u.ViewNumber).Take(5);
            if (tempTap2.Count() < 5)
            {
                ViewBag.Tap2 = randomArticle;
            }
            else
            {
                ViewBag.Tap2 = tempTap2;
            }
            /////////// END Tap2 ////////////

            /////////// Tap3 /////////////
            IQueryable<Article> tempTap3 = DataSingleton.Instance.GetLinQContext().Articles
                                                        .Where(
                                                                u => u.ArticleVisible == true
                                                                && (u.ArtStatusID == 3 || u.ArtStatusID == 4)
                                                                && (u.CreateDate > DateTime.Now.AddDays(-30))
                                                                )
                                                        .OrderByDescending(u => u.ViewNumber).Take(5);
            if (tempTap3.Count() < 5)
            {
                ViewBag.Tap3 = randomArticle;
            }
            else
            {
                ViewBag.Tap3 = tempTap3;
            }
            /////////// END Tap3 ////////////

            ViewBag.Tap4 = randomArticle;

            return View("/Views/FrontEnd/Home/Index.cshtml");
        }

        [ChildActionOnly]
        public ActionResult FirstArticleListHomePage()
        {
            return View("/Views/FrontEnd/Home/_ArticleListHomePage.cshtml", DataSingleton.Instance.GetArticleModels().GetArticleForHomePage(1));
        }


        public PartialViewResult ArticleListHomePage()
        {

            int page = 2;
            if (Request.Cookies[ConstantClass.cooPageNumberOfHomePage] != null && Request.Cookies[ConstantClass.cooPageNumberOfHomePage].Value != "")
            {
                try
                {
                    page = Convert.ToInt16(Request.Cookies[ConstantClass.cooPageNumberOfHomePage].Value);
                }
                catch (Exception)
                {
                    page = 2;
                }
            }

            IQueryable<Article> articleList = DataSingleton.Instance.GetArticleModels().GetArticleForHomePage(page);

            page = page + 1;
            if (Request.Cookies[ConstantClass.cooPageNumberOfHomePage] == null)
            {
                HttpCookie cookie = new HttpCookie(ConstantClass.cooPageNumberOfHomePage, page.ToString());
                Response.Cookies.Add(cookie);
                cookie.Expires = DateTime.Now.AddHours(1);
            }
            else
            {
                Response.Cookies[ConstantClass.cooPageNumberOfHomePage].Value = page.ToString();
            }

            //System.Threading.Thread.Sleep(1000);

            return PartialView("/Views/FrontEnd/Home/_ArticleListHomePage.cshtml", articleList);
        }

        [ChildActionOnly]
        public ActionResult SlideNewsView(IEnumerable<vngoodlife.Article> data)
        {
            return PartialView("/Views/FrontEnd/Home/_TapContent.cshtml", data);
        }



        public ActionResult GoToErrorPage(string errorType)
        {
            DataSingleton.Instance.GetLinQContext().Connection.Close();
            if (errorType == "404")
            {
                ViewBag.ErrorType = "Đường dẫn không chính xác. Xin vui lòng quay lại trang chủ.";
            }
            else
            {
                ViewBag.ErrorType = "Có lỗi xảy ra trên hệ thống. Xin vui lòng quay lại trang chủ.";
            }
            return View("/Views/Shared/Error.cshtml");
        }

    }
}
